﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewsWorm {
    internal partial class AddFeedDialog : Form {
        internal AddFeedDialog( IFeedManager manager ) {
            InitializeComponent();
            this.ButtonAdd.Enabled = false;
            this.Font = SystemFonts.MessageBoxFont;
            m_Manager = manager;
            PopulateHistory();
        }

        private String m_Url;
        private IFeedManager m_Manager;

        private void PopulateHistory() {
            this.comboBoxUrl.Items.Clear();

            foreach ( string url in m_Manager.LastFeedUrls ) {
                this.comboBoxUrl.Items.Add( url );
            }
        }

        //TODO: If the url starts with https, do not add http://
        internal String Url {
            get {

                if ( m_Url.StartsWith( "feed://" ) ) {
                    m_Url = m_Url.Replace( "feed://", "http://" );
                }

                if ( !m_Url.StartsWith( "http://" ) && !m_Url.StartsWith("https://") )
                    m_Url = "http://" + m_Url;

                return m_Url;
            }
        }

        private void ButtonAdd_Click( object sender, EventArgs e ) {
            m_Url = this.comboBoxUrl.Text;

            if ( !m_Manager.LastFeedUrls.Contains( m_Url ) )
                m_Manager.LastFeedUrls.Add( m_Url );

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void comboBoxUrl_TextChanged( object sender, EventArgs e ) {
            if ( this.comboBoxUrl.Text.Length > 0 )
                this.ButtonAdd.Enabled = true;
            else
                this.ButtonAdd.Enabled = false;
        }

        private void buttonClearUrls_Click( object sender, EventArgs e ) {
            m_Manager.LastFeedUrls.Clear();
            PopulateHistory();
        }

        public void SetUrl( string url ) {
            string u;

            if ( url.StartsWith( "newsworm://" ) )
                u = url.Replace( "newsworm://", "" );
            else
                u = url;

            this.comboBoxUrl.Text = u;
        }
    }
}
